package cnm.protocol;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.net.io.Util;
import org.apache.commons.net.telnet.TelnetClient;

import cnm.exception.SimulatorException;

// This class requires the IOUtil support class!
public class CliDeviceTelnet {

    public final static void main(String[] args) {
        String ip = "172.25.21.44";
        File cmdFile = new File("./metadata/conf/cmd.txt");
        File resultFile = new File("./metadata/result.txt");
        try {
            commucate(ip, cmdFile, resultFile);
        } catch (SimulatorException e) {
            e.printStackTrace();
            System.exit(1);
        }

        System.out.println("Finish validation...");
        System.out.println("Please fetch result in file:" + resultFile.getAbsolutePath());
        System.exit(0);
    }
    
    public static void commucate (String serverIp,File cmdFile, File resultFile) throws SimulatorException{
        int port = 23;
        String ip;
        
        if (serverIp.contains(":")) {
            port = Integer.getInteger(serverIp.split(":")[1]);
            ip = serverIp.split(":")[0];
        } else {
            ip = serverIp;
        }
        
        TelnetClient telnet = new TelnetClient();
        
        try {
            telnet.connect(ip, port);
        } catch (IOException e) {
            throw new SimulatorException("Can't connect to telnet server.",e);
        }

        try {
            BufferedInputStream buffIs = new BufferedInputStream(new FileInputStream(cmdFile));
            BufferedOutputStream buffOs = new BufferedOutputStream(new FileOutputStream(resultFile));
            readWrite(telnet.getInputStream(), telnet.getOutputStream(), buffIs, buffOs);
        } catch (FileNotFoundException e) {
            throw new SimulatorException("File not found.",e);
        }

        try {
            telnet.disconnect();
        } catch (IOException e) {
            throw new SimulatorException("Can't disconnect from telnet server.",e);
        }

    }

    public static final void readWrite(final InputStream remoteInput, final OutputStream remoteOutput,
            final InputStream localInput, final OutputStream localOutput) {
        Thread reader, writer;

        reader = new Thread() {
            public void run() {
                int ch;

                try {
                    Thread.sleep(6000);
                    ch = localInput.read();
                    while (!interrupted() && ch != -1) {
                        System.out.write(ch);
                        remoteOutput.write(ch);
                        remoteOutput.flush();
                        int temp = ch;
                        if ((ch = localInput.read()) != -1 && temp == 10) {
                            Thread.sleep(2000);
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        writer = new Thread() {
            public void run() {
                try {
                    Util.copyStream(remoteInput, localOutput);
                } catch (IOException e) {
                    e.printStackTrace();
                    System.exit(1);
                }
            }
        };

        writer.setPriority(Thread.currentThread().getPriority() + 1);

        writer.start();
        reader.setDaemon(true);
        reader.start();

        try {
            writer.join();
            reader.interrupt();
        } catch (InterruptedException e) {
        }
    }
}
